"""
API数据模型
定义请求和响应的数据结构
"""

from typing import List, Dict, Any, Optional
from datetime import datetime
from pydantic import BaseModel, Field


class HealthResponse(BaseModel):
    """健康检查响应"""
    status: str
    timestamp: datetime
    version: str


class CreateMeetingRequest(BaseModel):
    """创建会议请求"""
    meeting_id: str = Field(..., description="会议ID", min_length=1, max_length=100)
    title: Optional[str] = Field(None, description="会议标题", max_length=200)
    description: Optional[str] = Field(None, description="会议描述")


class UpdateMeetingStatusRequest(BaseModel):
    """更新会议状态请求"""
    status: str = Field(..., description="会议状态", regex="^(active|completed|archived)$")


class MeetingResponse(BaseModel):
    """会议响应"""
    id: int
    meeting_id: str
    title: Optional[str]
    description: Optional[str]
    start_time: Optional[str]
    end_time: Optional[str]
    status: str
    metadata: Optional[Dict[str, Any]]
    created_at: Optional[str]
    updated_at: Optional[str]


class TextResponse(BaseModel):
    """文本记录响应"""
    id: int
    text_id: str
    meeting_id: str
    content: str
    cleaned_content: Optional[str]
    source_type: str
    confidence: float
    timestamp: Optional[str]
    language: Optional[str]
    keywords: Optional[List[str]]
    region_ids: Optional[List[str]]
    metadata: Optional[Dict[str, Any]]
    created_at: Optional[str]


class SegmentResponse(BaseModel):
    """段落记录响应"""
    id: int
    segment_id: str
    meeting_id: str
    content: str
    source_type: str
    start_time: Optional[str]
    end_time: Optional[str]
    confidence: float
    keywords: Optional[List[str]]
    related_segments: Optional[List[str]]
    metadata: Optional[Dict[str, Any]]
    created_at: Optional[str]


class ExportRequest(BaseModel):
    """导出请求"""
    formats: List[str] = Field(..., description="导出格式列表")
    output_dir: Optional[str] = Field(None, description="输出目录")


class ExportResponse(BaseModel):
    """导出响应"""
    meeting_id: str
    formats: List[str]
    results: Dict[str, bool]
    export_time: datetime


class StatisticsResponse(BaseModel):
    """统计信息响应"""
    meeting_count: int
    text_count: int
    segment_count: int
    database_url: str
    system_info: Dict[str, Any]


class ErrorResponse(BaseModel):
    """错误响应"""
    error: str
    message: str
    timestamp: datetime


class StreamConfigRequest(BaseModel):
    """视频流配置请求"""
    stream_id: str = Field(..., description="流ID")
    stream_type: str = Field(..., description="流类型", regex="^(local_file|rtsp|webrtc|usb_camera|ip_camera)$")
    source: str = Field(..., description="视频源")
    fps: Optional[int] = Field(None, description="帧率", ge=1, le=60)
    resolution: Optional[List[int]] = Field(None, description="分辨率 [width, height]")
    buffer_size: Optional[int] = Field(100, description="缓冲区大小", ge=1, le=1000)
    auto_reconnect: Optional[bool] = Field(True, description="自动重连")


class StreamStatusResponse(BaseModel):
    """视频流状态响应"""
    stream_id: str
    status: str
    stream_type: str
    source: str
    fps: Optional[float]
    frame_count: int
    error_message: Optional[str]


class ProcessingStatsResponse(BaseModel):
    """处理统计响应"""
    detection_stats: Dict[str, Any]
    ocr_stats: Dict[str, Any]
    queue_stats: Dict[str, Any]
    processing_time: Dict[str, float]


class BatchProcessRequest(BaseModel):
    """批处理请求"""
    meeting_id: str = Field(..., description="会议ID")
    video_files: List[str] = Field(..., description="视频文件路径列表")
    output_formats: List[str] = Field(["json"], description="输出格式")
    processing_options: Optional[Dict[str, Any]] = Field(None, description="处理选项")


class BatchProcessResponse(BaseModel):
    """批处理响应"""
    task_id: str
    meeting_id: str
    status: str
    progress: float
    estimated_time: Optional[float]
    results: Optional[Dict[str, Any]]
